"""
对flask服务端发起post请求，预测图片
"""

import requests

PyTorch_REST_API_URL = 'http://127.0.0.1:5000/predict'
PyTorch_REST_API_URL = 'http://192.168.1.105:5000/predict'
PyTorch_REST_API_URL = 'http://192.168.1.105:80/predict'


def predict_result(image_path):
    # Initialize image path
    image = open(image_path, 'rb').read()
    payload = {'image': image}

    # Submit the request.
    r = requests.post(PyTorch_REST_API_URL, files=payload).json()

    # Ensure the request was successful.
    if r['success']:
        print(r['predictions'])
        # Loop over the predictions and display them.
        # for (i, result) in enumerate(r['predictions']):
        #     print('{}. {}: {:.4f}'.format(i + 1, result['label'],
        #                                   result['probability']))
    # Otherwise, the request failed.
    else:
        print('Request failed')


if __name__ == '__main__':
    predict_result(r'C:\Programs\workspace\deep_learning\data\pubg\stand_squat_lie\squat\20210901112508_1.jpg')
